package com.competitions.videoedithelper.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.competitions.videoedithelper.dao.UserInfoDao;
import com.competitions.videoedithelper.entity.ResponseModel;
import com.competitions.videoedithelper.entity.UserInfo;
import com.competitions.videoedithelper.service.UserInfoService;
import com.competitions.videoedithelper.utils.MD5Util;
import com.competitions.videoedithelper.utils.MailService;
import com.competitions.videoedithelper.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.UUID;

import static com.competitions.videoedithelper.utils.IpUtil.getIpAddr;

/**
 * @author Mapler
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    private UserInfoDao userInfoDao;
    private MailService mailService;

    @Autowired
    public UserInfoServiceImpl(UserInfoDao userInfoDao,MailService mailService) {
        this.mailService = mailService;
        this.userInfoDao = userInfoDao;
    }

    @Override
    public ResponseModel login(JSONObject data, HttpServletRequest request) {
        try {
            int roleId = Integer.parseInt(data.getOrDefault("roleId",-1).toString());
            String loginName = (String)data.getOrDefault("loginName",null);
            String loginPassword = (String)data.getOrDefault("loginPassword",null);
            loginPassword = MD5Util.getMD5String(loginPassword);
            UserInfo userInfo = userInfoDao.selectUserInfo(roleId,loginName,loginPassword);
            if(userInfo!=null){
                if(userInfo.getIsExamine() != 1){
                    return new ResponseModel(198,"登录失败，账号未审核");
                }
                if(userInfo.getStatus() == 1){
                    return new ResponseModel(197,"登录失败，账号已冻结");
                }
                if(userInfo.getStatus() == 2){
                    return new ResponseModel(196,"登录失败，账号已删除");
                }
                String token = TokenUtil.createToken(String.valueOf(userInfo.getUserId()),userInfo.getUserName());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date = simpleDateFormat.format(System.currentTimeMillis());
                String ip = getIpAddr(request);
                mailService.sendSimpleMail("a1750111263@qq.com","语音教学系统登录",roleId == 1?"管理员登录":"普通用户登录");
                userInfoDao.updateUserLogin(userInfo.getUserId(), date, ip, token);
                return new ResponseModel(200,"登录成功",userInfo);
            }
            return new ResponseModel(199,"用户名或密码错误");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel registerAccount(JSONObject data) {
        try {
            String loginName = (String)data.getOrDefault("loginName",null);
            String loginPassword = (String)data.getOrDefault("loginPassword",null);
            loginPassword = MD5Util.getMD5String(loginPassword);
            String userName = (String)data.getOrDefault("userName",null);
            String tel = (String)data.getOrDefault("tel",null);
            String email = (String)data.getOrDefault("email",null);
            int bindingRole = (Integer) data.getOrDefault("bindingRole",-1);
            String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis());
            String uuid = UUID.randomUUID().toString().substring(19);
            int roleId = (Integer) data.getOrDefault("roleId",-1);
            userInfoDao.insertUser(loginName,loginPassword,userName,createTime,email,tel,bindingRole,uuid,roleId);
            return new ResponseModel(200,"注册成功，等待管理员审核");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel updateUserInfo(JSONObject data) {
        try {
            int uid = (Integer) data.getOrDefault("uid",-1);
            UserInfo userInfo = userInfoDao.selectUserInfoByUid(uid);
            if(userInfo == null){
                return new ResponseModel(195,"更新失败，用户不存在");
            }
            String userName = (String)data.getOrDefault("userName",userInfo.getUserName());
            String tel = (String)data.getOrDefault("tel",userInfo.getTel());
            String email = (String)data.getOrDefault("email",userInfo.getEmail());
            userInfoDao.updateUserInfo(uid,userName,email,tel);
            return new ResponseModel(200,"修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel registerUserList() {
        try {
            return new ResponseModel(200, "获取成功", userInfoDao.selectRegisterUserInfo());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel receiveRegister(int userId) {
        try {
            userInfoDao.updateIsExamine(userId);
            return new ResponseModel(200,"操作成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel rejectRegister(int userId) {
        try {
            userInfoDao.deleteRegisterUserInfo(userId);
            return new ResponseModel(200,"操作成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel selectUsers() {
        try {
            return new ResponseModel(200, "获取成功", userInfoDao.selectUsers());
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }

    @Override
    public ResponseModel setUserStatus(int userId, int status) {
        try {
            userInfoDao.updateStatus(userId,status);
            return new ResponseModel(200,"操作成功");
        }catch (Exception e) {
            e.printStackTrace();
            return new ResponseModel(500,"未知错误");
        }
    }
}
